home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / FunTetris / funtetris.jar / gratest / TetrisCanvas.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-05-20  |  13.8 KB  |  634 lines

  1. package gratest;
  2.  
  3. import java.util.Random;
  4. import java.util.Timer;
  5. import javax.microedition.lcdui.Canvas;
  6. import javax.microedition.lcdui.Font;
  7. import javax.microedition.lcdui.Graphics;
  8. import javax.microedition.lcdui.Image;
  9.  
  10. class TetrisCanvas extends Canvas {
  11.    private int iCurrentObject;
  12.    private int iCurrentPosX;
  13.    private int iCurrentPosY;
  14.    private int iCurrentObjectRotation;
  15.    private int iScreenWidth;
  16.    private int iScreenHeight;
  17.    private int iPlayWidth;
  18.    private int iBoxSize;
  19.    private int iNumBoxesWidth = 12;
  20.    private int iNumBoxesHeight;
  21.    private int iWidthPercent = 50;
  22.    private int iPlayHeight;
  23.    private TetElements[] elements;
  24.    private int[][] table;
  25.    protected Timer tetTimer;
  26.    protected Timer dropTimer;
  27.    private Random rand = new Random(System.currentTimeMillis());
  28.    private int iLevelSpeed;
  29.    private int iDropSpeed;
  30.    private int iCurrentSpeed;
  31.    private int iTimerSem = 0;
  32.    private int iScore = 0;
  33.    private int firstIteration = 0;
  34.    private int iTopLine;
  35.    private int iEndGame = 0;
  36.    private int iCurrentLevel = 1;
  37.    Image mainImage;
  38.    Graphics mainGraphics;
  39.  
  40.    public TetrisCanvas(int iNumBoxesParam, int iLevelParam) {
  41.       this.iCurrentLevel = iLevelParam;
  42.       this.iNumBoxesWidth = iNumBoxesParam;
  43.       this.iLevelSpeed = 430 - iLevelParam * 40;
  44.       this.iDropSpeed = 30;
  45.       this.iCurrentSpeed = this.iLevelSpeed;
  46.       this.iScreenWidth = ((Canvas)this).getWidth();
  47.       this.iScreenHeight = ((Canvas)this).getHeight();
  48.       this.iPlayWidth = this.iScreenHeight * this.iWidthPercent / 100;
  49.       this.iBoxSize = this.iPlayWidth / this.iNumBoxesWidth;
  50.       this.iPlayWidth = this.iBoxSize * this.iNumBoxesWidth;
  51.       this.iNumBoxesHeight = this.iScreenHeight / this.iBoxSize;
  52.       this.iTopLine = this.iNumBoxesHeight;
  53.       this.iPlayHeight = this.iNumBoxesHeight * this.iBoxSize;
  54.       this.table = new int[this.iNumBoxesHeight + 1][this.iNumBoxesWidth + 2];
  55.  
  56.       for(int i = 0; i < this.iNumBoxesWidth + 2; ++i) {
  57.          this.table[this.iNumBoxesHeight][i] = 1;
  58.       }
  59.  
  60.       for(int i = 0; i < this.iNumBoxesHeight + 1; ++i) {
  61.          this.table[i][0] = 1;
  62.          this.table[i][this.iNumBoxesWidth + 1] = 1;
  63.       }
  64.  
  65.       this.mainImage = Image.createImage(this.iScreenWidth, this.iScreenHeight);
  66.       this.mainGraphics = this.mainImage.getGraphics();
  67.       this.mainGraphics.setColor(255, 255, 255);
  68.       this.mainGraphics.fillRect(0, 0, this.iScreenWidth, this.iScreenHeight);
  69.       this.mainGraphics.setColor(0, 255, 0);
  70.       this.mainGraphics.fillRect(this.iPlayWidth, 0, 8, this.iScreenHeight);
  71.       this.mainGraphics.fillRect(0, this.iPlayHeight, this.iPlayWidth + 9, 8);
  72.       this.mainGraphics.setColor(0, 0, 0);
  73.       Font font = Font.getFont(32, 1, 8);
  74.       this.mainGraphics.setFont(font);
  75.       this.mainGraphics.drawString("Classic", this.iPlayWidth + 10, 10, 20);
  76.       this.mainGraphics.drawString("Tetris", this.iPlayWidth + 10, 20, 20);
  77.       this.mainGraphics.drawString("Level:", this.iPlayWidth + 10, 30, 20);
  78.       Integer iTemp = new Integer(this.iCurrentLevel);
  79.       this.mainGraphics.drawString(iTemp.toString(), this.iPlayWidth + 10, 40, 20);
  80.       this.mainGraphics.drawString("Score:", this.iPlayWidth + 10, 50, 20);
  81.       this.elements = new TetElements[7];
  82.       this.elements[0] = new TetElements(5, 2);
  83.       this.elements[0].SetPosition(0, 0, 0);
  84.       this.elements[0].SetPosition(0, 1, 5);
  85.       this.elements[0].SetPosition(0, 2, 10);
  86.       this.elements[0].SetPosition(0, 3, 15);
  87.       this.elements[0].SetPosition(0, 4, 20);
  88.       this.elements[0].SetBottomConstraintValues(0, 1, -1, 0, 0, 0, 0);
  89.       this.elements[0].SetLeftConstraintValues(0, 5, -1, -2, -3, -4, -5);
  90.       this.elements[0].SetRightConstraintValues(0, 5, 1, 6, 11, 16, 21);
  91.       this.elements[0].SetRotMove(0, -2, 2);
  92.       this.elements[0].SetRotConstraintValues(0, 0, 4, -11, -12, 11, 12, 0);
  93.       this.elements[0].SetPosition(1, 0, 0);
  94.       this.elements[0].SetPosition(1, 1, 1);
  95.       this.elements[0].SetPosition(1, 2, 2);
  96.       this.elements[0].SetPosition(1, 3, 3);
  97.       this.elements[0].SetPosition(1, 4, 4);
  98.       this.elements[0].SetBottomConstraintValues(1, 5, 5, 6, 7, 8, 9);
  99.       this.elements[0].SetLeftConstraintValues(1, 1, -1, 0, 0, 0, 0);
  100.       this.elements[0].SetRightConstraintValues(1, 1, -1, 0, 0, 0, 0);
  101.       this.elements[0].SetRotMove(1, 2, -2);
  102.       this.elements[0].SetRotConstraintValues(1, 0, 4, 7, 12, -111, -112, 0);
  103.       this.elements[1] = new TetElements(4, 2);
  104.       this.elements[1].SetPosition(0, 0, 1);
  105.       this.elements[1].SetPosition(0, 1, 2);
  106.       this.elements[1].SetPosition(0, 2, 5);
  107.       this.elements[1].SetPosition(0, 3, 6);
  108.       this.elements[1].SetBottomConstraintValues(0, 3, 7, 10, 11, 0, 0);
  109.       this.elements[1].SetLeftConstraintValues(0, 2, 0, -2, 0, 0, 0);
  110.       this.elements[1].SetRightConstraintValues(0, 2, 3, 7, 0, 0, 0);
  111.       this.elements[1].SetRotConstraintValues(0, 0, 2, 0, 11, 0, 0, 0);
  112.       this.elements[1].SetPosition(1, 0, 0);
  113.       this.elements[1].SetPosition(1, 1, 5);
  114.       this.elements[1].SetPosition(1, 2, 6);
  115.       this.elements[1].SetPosition(1, 3, 11);
  116.       this.elements[1].SetBottomConstraintValues(1, 2, 10, 16, 0, 0, 0);
  117.       this.elements[1].SetLeftConstraintValues(1, 3, -1, -2, 10, 0, 0);
  118.       this.elements[1].SetRightConstraintValues(1, 3, 1, 7, 12, 0, 0);
  119.       this.elements[1].SetRotConstraintValues(1, 0, 2, 1, 2, 0, 0, 0);
  120.       this.elements[2] = new TetElements(4, 1);
  121.       this.elements[2].SetPosition(0, 0, 0);
  122.       this.elements[2].SetPosition(0, 1, 1);
  123.       this.elements[2].SetPosition(0, 2, 5);
  124.       this.elements[2].SetPosition(0, 3, 6);
  125.       this.elements[2].SetBottomConstraintValues(0, 2, 10, 11, 0, 0, 0);
  126.       this.elements[2].SetLeftConstraintValues(0, 2, -1, -2, 0, 0, 0);
  127.       this.elements[2].SetRightConstraintValues(0, 2, 2, 7, 0, 0, 0);
  128.       this.elements[3] = new TetElements(4, 4);
  129.       this.elements[3].SetPosition(0, 0, 0);
  130.       this.elements[3].SetPosition(0, 1, 1);
  131.       this.elements[3].SetPosition(0, 2, 2);
  132.       this.elements[3].SetPosition(0, 3, 6);
  133.       this.elements[3].SetBottomConstraintValues(0, 3, 5, 7, 11, 0, 0);
  134.       this.elements[3].SetLeftConstraintValues(0, 2, -1, 5, 0, 0, 0);
  135.       this.elements[3].SetRightConstraintValues(0, 2, 3, 6, 0, 0, 0);
  136.       this.elements[3].SetRotConstraintValues(0, 0, 2, 5, 10, 0, 0, 0);
  137.       this.elements[3].SetPosition(1, 0, 0);
  138.       this.elements[3].SetPosition(1, 1, 5);
  139.       this.elements[3].SetPosition(1, 2, 6);
  140.       this.elements[3].SetPosition(1, 3, 10);
  141.       this.elements[3].SetBottomConstraintValues(1, 2, 11, 15, 0, 0, 0);
  142.       this.elements[3].SetLeftConstraintValues(1, 3, -1, -2, -3, 0, 0);
  143.       this.elements[3].SetRightConstraintValues(1, 3, 1, 7, 11, 0, 0);
  144.       this.elements[3].SetRotConstraintValues(1, 0, 2, 1, 7, 0, 0, 0);
  145.       this.elements[3].SetPosition(2, 0, 1);
  146.       this.elements[3].SetPosition(2, 1, 5);
  147.       this.elements[3].SetPosition(2, 2, 6);
  148.       this.elements[3].SetPosition(2, 3, 7);
  149.       this.elements[3].SetBottomConstraintValues(2, 3, 10, 11, 12, 0, 0);
  150.       this.elements[3].SetLeftConstraintValues(2, 2, 0, -2, 0, 0, 0);
  151.       this.elements[3].SetRightConstraintValues(2, 2, 2, 8, 0, 0, 0);
  152.       this.elements[3].SetRotConstraintValues(2, 0, 1, 11, 0, 0, 0, 0);
  153.       this.elements[3].SetPosition(3, 0, 1);
  154.       this.elements[3].SetPosition(3, 1, 5);
  155.       this.elements[3].SetPosition(3, 2, 6);
  156.       this.elements[3].SetPosition(3, 3, 11);
  157.       this.elements[3].SetBottomConstraintValues(3, 2, 10, 16, 0, 0, 0);
  158.       this.elements[3].SetLeftConstraintValues(3, 3, 0, -2, 10, 0, 0);
  159.       this.elements[3].SetRightConstraintValues(3, 3, 2, 7, 12, 0, 0);
  160.       this.elements[3].SetRotConstraintValues(3, 0, 2, 0, 2, 0, 0, 0);
  161.       this.elements[4] = new TetElements(4, 4);
  162.       this.elements[4].SetPosition(0, 0, 2);
  163.       this.elements[4].SetPosition(0, 1, 5);
  164.       this.elements[4].SetPosition(0, 2, 6);
  165.       this.elements[4].SetPosition(0, 3, 7);
  166.       this.elements[4].SetBottomConstraintValues(0, 3, 10, 11, 12, 0, 0);
  167.       this.elements[4].SetLeftConstraintValues(0, 2, 1, -2, 0, 0, 0);
  168.       this.elements[4].SetRightConstraintValues(0, 2, 3, 8, 0, 0, 0);
  169.       this.elements[4].SetRotConstraintValues(0, 0, 3, 0, 10, 11, 0, 0);
  170.       this.elements[4].SetPosition(1, 0, 0);
  171.       this.elements[4].SetPosition(1, 1, 5);
  172.       this.elements[4].SetPosition(1, 2, 10);
  173.       this.elements[4].SetPosition(1, 3, 11);
  174.       this.elements[4].SetBottomConstraintValues(1, 2, 15, 16, 0, 0, 0);
  175.       this.elements[4].SetLeftConstraintValues(1, 3, -1, -2, -3, 0, 0);
  176.       this.elements[4].SetRightConstraintValues(1, 3, 1, 6, 12, 0, 0);
  177.       this.elements[4].SetRotConstraintValues(1, 0, 2, 1, 2, 0, 0, 0);
  178.       this.elements[4].SetPosition(2, 0, 0);
  179.       this.elements[4].SetPosition(2, 1, 1);
  180.       this.elements[4].SetPosition(2, 2, 2);
  181.       this.elements[4].SetPosition(2, 3, 5);
  182.       this.elements[4].SetBottomConstraintValues(2, 3, 6, 7, 10, 0, 0);
  183.       this.elements[4].SetLeftConstraintValues(2, 2, -1, -2, 0, 0, 0);
  184.       this.elements[4].SetRightConstraintValues(2, 2, 3, 6, 0, 0, 0);
  185.       this.elements[4].SetRotConstraintValues(2, 0, 2, 6, 11, 0, 0, 0);
  186.       this.elements[4].SetPosition(3, 0, 0);
  187.       this.elements[4].SetPosition(3, 1, 1);
  188.       this.elements[4].SetPosition(3, 2, 6);
  189.       this.elements[4].SetPosition(3, 3, 11);
  190.       this.elements[4].SetBottomConstraintValues(3, 2, 5, 16, 0, 0, 0);
  191.       this.elements[4].SetLeftConstraintValues(3, 3, -1, 5, 10, 0, 0);
  192.       this.elements[4].SetRightConstraintValues(3, 3, 2, 7, 12, 0, 0);
  193.       this.elements[4].SetRotConstraintValues(3, 0, 3, 5, 7, 2, 0, 0);
  194.       this.elements[5] = new TetElements(4, 4);
  195.       this.elements[5].SetPosition(0, 0, 0);
  196.       this.elements[5].SetPosition(0, 1, 5);
  197.       this.elements[5].SetPosition(0, 2, 6);
  198.       this.elements[5].SetPosition(0, 3, 7);
  199.       this.elements[5].SetBottomConstraintValues(0, 3, 10, 11, 12, 0, 0);
  200.       this.elements[5].SetLeftConstraintValues(0, 2, -1, -2, 0, 0, 0);
  201.       this.elements[5].SetRightConstraintValues(0, 2, 1, 8, 0, 0, 0);
  202.       this.elements[5].SetRotConstraintValues(0, 0, 2, 1, 10, 0, 0, 0);
  203.       this.elements[5].SetPosition(1, 0, 0);
  204.       this.elements[5].SetPosition(1, 1, 1);
  205.       this.elements[5].SetPosition(1, 2, 5);
  206.       this.elements[5].SetPosition(1, 3, 10);
  207.       this.elements[5].SetBottomConstraintValues(1, 2, 15, 6, 0, 0, 0);
  208.       this.elements[5].SetLeftConstraintValues(1, 3, -1, -2, -3, 0, 0);
  209.       this.elements[5].SetRightConstraintValues(1, 3, 2, 6, 11, 0, 0);
  210.       this.elements[5].SetRotConstraintValues(1, 0, 2, 2, 7, 0, 0, 0);
  211.       this.elements[5].SetPosition(2, 0, 0);
  212.       this.elements[5].SetPosition(2, 1, 1);
  213.       this.elements[5].SetPosition(2, 2, 2);
  214.       this.elements[5].SetPosition(2, 3, 7);
  215.       this.elements[5].SetBottomConstraintValues(2, 3, 5, 6, 12, 0, 0);
  216.       this.elements[5].SetLeftConstraintValues(2, 2, -1, 6, 0, 0, 0);
  217.       this.elements[5].SetRightConstraintValues(2, 2, 3, 8, 0, 0, 0);
  218.       this.elements[5].SetRotConstraintValues(2, 0, 3, 6, 10, 11, 0, 0);
  219.       this.elements[5].SetPosition(3, 0, 1);
  220.       this.elements[5].SetPosition(3, 1, 6);
  221.       this.elements[5].SetPosition(3, 2, 10);
  222.       this.elements[5].SetPosition(3, 3, 11);
  223.       this.elements[5].SetBottomConstraintValues(3, 2, 15, 16, 0, 0, 0);
  224.       this.elements[5].SetLeftConstraintValues(3, 3, 0, 5, -2, 0, 0);
  225.       this.elements[5].SetRightConstraintValues(3, 3, 2, 7, 11, 0, 0);
  226.       this.elements[5].SetRotConstraintValues(3, 0, 3, 0, 5, 7, 0, 0);
  227.       this.elements[6] = new TetElements(4, 2);
  228.       this.elements[6].SetPosition(0, 0, 0);
  229.       this.elements[6].SetPosition(0, 1, 1);
  230.       this.elements[6].SetPosition(0, 2, 6);
  231.       this.elements[6].SetPosition(0, 3, 7);
  232.       this.elements[6].SetBottomConstraintValues(0, 3, 5, 11, 12, 0, 0);
  233.       this.elements[6].SetLeftConstraintValues(0, 2, -1, 5, 0, 0, 0);
  234.       this.elements[6].SetRightConstraintValues(0, 2, 2, 8, 0, 0, 0);
  235.       this.elements[6].SetRotConstraintValues(0, 0, 2, 5, 10, 0, 0, 0);
  236.       this.elements[6].SetPosition(1, 0, 1);
  237.       this.elements[6].SetPosition(1, 1, 5);
  238.       this.elements[6].SetPosition(1, 2, 6);
  239.       this.elements[6].SetPosition(1, 3, 10);
  240.       this.elements[6].SetBottomConstraintValues(1, 2, 11, 15, 0, 0, 0);
  241.       this.elements[6].SetLeftConstraintValues(1, 3, 0, -2, -3, 0, 0);
  242.       this.elements[6].SetRightConstraintValues(1, 3, 2, 7, 11, 0, 0);
  243.       this.elements[6].SetRotConstraintValues(1, 0, 2, 0, 7, 0, 0, 0);
  244.       this.iCurrentObject = 1;
  245.       this.iCurrentPosX = this.iNumBoxesWidth / 2;
  246.       this.iCurrentPosY = 0;
  247.       this.iCurrentObjectRotation = 0;
  248.  
  249.       int iRand;
  250.       for(iRand = -1; iRand < 0; iRand = this.rand.nextInt()) {
  251.       }
  252.  
  253.       this.iCurrentObject = iRand % 7;
  254.       this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 0, 255, 255, this.mainGraphics);
  255.       this.tetTimer = new Timer();
  256.       this.tetTimer.schedule(new TetrisTask(this), 0L, (long)this.iLevelSpeed);
  257.    }
  258.  
  259.    public void drawElement(int element, int elementrot, int x, int y, int r, int g, int b, Graphics gr) {
  260.       if (r == 255 && g == 255 && b == 255) {
  261.          gr.setColor(r, g, b);
  262.       } else if (this.iCurrentObject == 0) {
  263.          gr.setColor(0, 0, 0);
  264.       } else if (this.iCurrentObject == 1) {
  265.          gr.setColor(255, 0, 255);
  266.       } else if (this.iCurrentObject == 2) {
  267.          gr.setColor(0, 0, 255);
  268.       } else if (this.iCurrentObject == 3) {
  269.          gr.setColor(0, 255, 255);
  270.       } else if (this.iCurrentObject == 4) {
  271.          gr.setColor(0, 166, 88);
  272.       } else if (this.iCurrentObject == 5) {
  273.          gr.setColor(166, 88, 255);
  274.       } else if (this.iCurrentObject == 6) {
  275.          gr.setColor(166, 88, 88);
  276.       }
  277.  
  278.       TetElements Tet = this.elements[element];
  279.       int iNumBlocs = Tet.iNumBlocs;
  280.  
  281.       for(int i = 0; i < iNumBlocs; ++i) {
  282.          int value = Tet.GetPosition(elementrot, i);
  283.          int xrect = value % 5;
  284.          int yrect = value / 5;
  285.          int xpos = x * this.iBoxSize + xrect * this.iBoxSize;
  286.          int ypos = y * this.iBoxSize + yrect * this.iBoxSize;
  287.          gr.fillRect(xpos, ypos, this.iBoxSize - 1, this.iBoxSize - 1);
  288.       }
  289.  
  290.    }
  291.  
  292.    public void cutLine(int iLine) {
  293.       Image imagetemp = Image.createImage(this.iScreenWidth, this.iScreenHeight);
  294.       Graphics gtemp = imagetemp.getGraphics();
  295.       gtemp.drawImage(this.mainImage, 0, 0, 20);
  296.       this.mainGraphics.setClip(0, 0, this.iPlayWidth, iLine * this.iBoxSize);
  297.       this.mainGraphics.setColor(255, 255, 255);
  298.       this.mainGraphics.fillRect(0, 0, this.iScreenWidth, this.iBoxSize);
  299.       this.mainGraphics.drawImage(imagetemp, 0, this.iBoxSize, 20);
  300.       this.mainGraphics.setClip(0, 0, this.iScreenWidth, this.iScreenHeight);
  301.    }
  302.  
  303.    public int MoveTable(int iLine) {
  304.       for(int i = iLine - 1; i > -1; --i) {
  305.          for(int j = 1; j < this.iNumBoxesWidth + 1; ++j) {
  306.             this.table[i + 1][j] = this.table[i][j];
  307.          }
  308.       }
  309.  
  310.       return 0;
  311.    }
  312.  
  313.    public int checkFullLine(int iBottomLine, int iNumLinesToCheck) {
  314.       int iEmpty = 0;
  315.       int iInd = 0;
  316.  
  317.       for(int i = 0; i < iNumLinesToCheck; ++i) {
  318.          if (iInd == 1 && iEmpty == 0) {
  319.             return i - 1;
  320.          }
  321.  
  322.          iEmpty = 0;
  323.  
  324.          for(int j = 1; j < this.iNumBoxesWidth + 1; ++j) {
  325.             iInd = 1;
  326.             if (iBottomLine - i < 0) {
  327.                return -1;
  328.             }
  329.  
  330.             if (this.table[iBottomLine - i][j] == 0) {
  331.                iEmpty = 1;
  332.             }
  333.          }
  334.       }
  335.  
  336.       return -1;
  337.    }
  338.  
  339.    public int CalculateNext() {
  340.       int iGo = 1;
  341.       TetElements Tet = this.elements[this.iCurrentObject];
  342.       int iNumConstraints = Tet.GetBottomConstraintValue(this.iCurrentObjectRotation, 0);
  343.  
  344.       for(int j = 0; j < Tet.iNumBlocs; ++j) {
  345.          int value = Tet.GetPosition(this.iCurrentObjectRotation, j);
  346.          int xrect = value % 5;
  347.          int yrect = value / 5;
  348.          int xpos = this.iCurrentPosX + 1 + xrect;
  349.          int ypos1 = this.iCurrentPosY + yrect;
  350.          if (this.table[ypos1][xpos] == 1) {
  351.             this.mainGraphics.setColor(0, 0, 0);
  352.             this.mainGraphics.drawString("Game Over", 10, 50, 20);
  353.             this.iEndGame = 1;
  354.             this.tetTimer.cancel();
  355.          }
  356.       }
  357.  
  358.       for(int i = 0; i < iNumConstraints; ++i) {
  359.          int iValue = Tet.GetBottomConstraintValue(this.iCurrentObjectRotation, i + 1);
  360.          if (iValue < 0) {
  361.             if (this.table[this.iCurrentPosY + 5][this.iCurrentPosX - iValue] == 1) {
  362.                iGo = 0;
  363.             }
  364.          } else {
  365.             int x = iValue % 5;
  366.             int y = iValue / 5;
  367.             if (this.table[this.iCurrentPosY + y][this.iCurrentPosX + 1 + x] == 1) {
  368.                iGo = 0;
  369.             }
  370.          }
  371.       }
  372.  
  373.       if (iGo == 1) {
  374.          this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 255, 255, 255, this.mainGraphics);
  375.          ++this.iCurrentPosY;
  376.          ++this.firstIteration;
  377.          this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 0, 255, 255, this.mainGraphics);
  378.       }
  379.  
  380.       if (iGo == 0) {
  381.          this.firstIteration = 0;
  382.          int ypos = 0;
  383.  
  384.          for(int j = 0; j < Tet.iNumBlocs; ++j) {
  385.             int value = Tet.GetPosition(this.iCurrentObjectRotation, j);
  386.             int xrect = value % 5;
  387.             int yrect = value / 5;
  388.             int xpos = this.iCurrentPosX + 1 + xrect;
  389.             ypos = this.iCurrentPosY + yrect;
  390.             this.table[ypos][xpos] = 1;
  391.             if (ypos < this.iTopLine) {
  392.                this.iTopLine = ypos;
  393.             }
  394.          }
  395.  
  396.          int iEnd = 6;
  397.          int iLineScore = 0;
  398.  
  399.          while(iEnd > 0) {
  400.             int iRes = this.checkFullLine(ypos, iEnd);
  401.             if (iRes > -1) {
  402.                ypos -= iRes;
  403.                this.cutLine(ypos + 1);
  404.                this.MoveTable(ypos);
  405.                iEnd = iEnd - 1 - iRes;
  406.                ++iLineScore;
  407.                ++this.iTopLine;
  408.             } else {
  409.                --ypos;
  410.                --iEnd;
  411.             }
  412.          }
  413.  
  414.          if (iLineScore < 3) {
  415.             this.iScore += iLineScore * 50;
  416.          } else {
  417.             this.iScore += iLineScore * 200;
  418.          }
  419.  
  420.          if (iLineScore == 0) {
  421.             this.iScore += 4;
  422.          }
  423.  
  424.          if (this.iScore > this.iCurrentLevel * 1000) {
  425.             if (this.iLevelSpeed > 100) {
  426.                this.iLevelSpeed -= 40;
  427.             }
  428.  
  429.             ++this.iCurrentLevel;
  430.             Integer iTemp1 = new Integer(this.iCurrentLevel);
  431.             this.mainGraphics.setColor(255, 255, 255);
  432.             this.mainGraphics.fillRect(this.iPlayWidth + 10, 40, 50, 10);
  433.             this.mainGraphics.setColor(0, 0, 0);
  434.             this.mainGraphics.drawString(iTemp1.toString(), this.iPlayWidth + 10, 40, 20);
  435.          }
  436.  
  437.          int iRand;
  438.          for(iRand = -1; iRand < 0; iRand = this.rand.nextInt()) {
  439.          }
  440.  
  441.          this.iCurrentObject = iRand % 7;
  442.          if (this.iCurrentObject < 0) {
  443.             this.iCurrentObject = 1;
  444.          }
  445.  
  446.          this.iCurrentObjectRotation = 0;
  447.          this.iCurrentPosX = 6;
  448.          this.iCurrentPosY = 0;
  449.          this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 0, 255, 255, this.mainGraphics);
  450.          Integer iTempScore = new Integer(this.iScore);
  451.          this.mainGraphics.setColor(255, 255, 255);
  452.          this.mainGraphics.fillRect(this.iPlayWidth + 10, 60, 50, 10);
  453.          this.mainGraphics.setColor(0, 0, 0);
  454.          this.mainGraphics.drawString(iTempScore.toString(), this.iPlayWidth + 10, 60, 20);
  455.          if (this.iCurrentSpeed == this.iDropSpeed) {
  456.             this.iCurrentSpeed = this.iLevelSpeed;
  457.             this.dropTimer.cancel();
  458.             this.tetTimer = new Timer();
  459.             this.tetTimer.schedule(new TetrisTask(this), 0L, (long)this.iCurrentSpeed);
  460.             this.iTimerSem = 0;
  461.          }
  462.       }
  463.  
  464.       return 1;
  465.    }
  466.  
  467.    public int CalculateLeft() {
  468.       int iGo = 1;
  469.       TetElements Tet = this.elements[this.iCurrentObject];
  470.       int iNumConstraints = Tet.GetLeftConstraintValue(this.iCurrentObjectRotation, 0);
  471.  
  472.       for(int i = 0; i < iNumConstraints; ++i) {
  473.          int iValue = Tet.GetLeftConstraintValue(this.iCurrentObjectRotation, i + 1);
  474.          if (iValue < 0) {
  475.             if (this.table[this.iCurrentPosY - iValue - 1][this.iCurrentPosX] == 1) {
  476.                iGo = 0;
  477.                break;
  478.             }
  479.          } else {
  480.             int x = iValue % 5;
  481.             int y = iValue / 5;
  482.             if (this.table[this.iCurrentPosY + y][this.iCurrentPosX + 1 + x] == 1) {
  483.                iGo = 0;
  484.                break;
  485.             }
  486.          }
  487.       }
  488.  
  489.       if (iGo == 1) {
  490.          this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 255, 255, 255, this.mainGraphics);
  491.          --this.iCurrentPosX;
  492.          this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 0, 255, 255, this.mainGraphics);
  493.       }
  494.  
  495.       return 1;
  496.    }
  497.  
  498.    public int CalculateRight() {
  499.       int iGo = 1;
  500.       TetElements Tet = this.elements[this.iCurrentObject];
  501.       int iNumConstraints = Tet.GetRightConstraintValue(this.iCurrentObjectRotation, 0);
  502.  
  503.       for(int i = 0; i < iNumConstraints; ++i) {
  504.          int iValue = Tet.GetRightConstraintValue(this.iCurrentObjectRotation, i + 1);
  505.          if (iValue < 0) {
  506.             if (this.table[this.iCurrentPosY - iValue - 1][this.iCurrentPosX + 6] == 1) {
  507.                iGo = 0;
  508.                break;
  509.             }
  510.          } else {
  511.             int x = iValue % 5;
  512.             int y = iValue / 5;
  513.             if (this.table[this.iCurrentPosY + y][this.iCurrentPosX + 1 + x] == 1) {
  514.                iGo = 0;
  515.                break;
  516.             }
  517.          }
  518.       }
  519.  
  520.       if (iGo == 1) {
  521.          this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 255, 255, 255, this.mainGraphics);
  522.          ++this.iCurrentPosX;
  523.          this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 0, 255, 255, this.mainGraphics);
  524.       }
  525.  
  526.       return 1;
  527.    }
  528.  
  529.    public int CalculateRotate() {
  530.       int iGo = 1;
  531.       TetElements Tet = this.elements[this.iCurrentObject];
  532.       if (Tet.iNoRotConstraints == 1) {
  533.          return iGo;
  534.       } else {
  535.          int iNumConstraints = Tet.GetRotConstraintValue(this.iCurrentObjectRotation, 0);
  536.  
  537.          for(int i = 0; i < iNumConstraints; ++i) {
  538.             int iValue = Tet.GetRotConstraintValue(this.iCurrentObjectRotation, i + 1);
  539.             if (iValue < 0) {
  540.                int iYChanges = 0;
  541.                iValue = 0 - iValue;
  542.                if (iValue > 100) {
  543.                   iYChanges = 1;
  544.                   iValue -= 100;
  545.                }
  546.  
  547.                int x = iValue % 5;
  548.                int y = iValue / 5;
  549.                int xpos;
  550.                int ypos;
  551.                if (iYChanges == 0) {
  552.                   ypos = this.iCurrentPosY + y;
  553.                   xpos = this.iCurrentPosX + 1 - x;
  554.                } else {
  555.                   ypos = this.iCurrentPosY - y;
  556.                   xpos = this.iCurrentPosX + 1 + x;
  557.                }
  558.  
  559.                if (this.table[ypos][xpos] == 1) {
  560.                   iGo = 0;
  561.                   break;
  562.                }
  563.             } else {
  564.                int x = iValue % 5;
  565.                int y = iValue / 5;
  566.                if (this.table[this.iCurrentPosY + y][this.iCurrentPosX + 1 + x] == 1) {
  567.                   iGo = 0;
  568.                   break;
  569.                }
  570.             }
  571.          }
  572.  
  573.          return iGo;
  574.       }
  575.    }
  576.  
  577.    public void Rotate() {
  578.       int iCanRotate = this.CalculateRotate();
  579.       if (iCanRotate != 0) {
  580.          TetElements Tet = this.elements[this.iCurrentObject];
  581.          int iNumRotations = Tet.iNumRotations;
  582.          int iMoveX = Tet.rotMove[this.iCurrentObjectRotation][0];
  583.          int iMoveY = Tet.rotMove[this.iCurrentObjectRotation][1];
  584.          if (iMoveX == -10) {
  585.             iMoveX = 0;
  586.             iMoveY = 0;
  587.          }
  588.  
  589.          if (iNumRotations > 1) {
  590.             this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 255, 255, 255, this.mainGraphics);
  591.             ++this.iCurrentObjectRotation;
  592.             if (this.iCurrentObjectRotation > iNumRotations - 1) {
  593.                this.iCurrentObjectRotation = 0;
  594.             }
  595.  
  596.             this.iCurrentPosX += iMoveX;
  597.             this.iCurrentPosY += iMoveY;
  598.             this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 0, 255, 255, this.mainGraphics);
  599.          }
  600.  
  601.       }
  602.    }
  603.  
  604.    public void paint(Graphics g) {
  605.       g.drawImage(this.mainImage, 0, 0, 20);
  606.    }
  607.  
  608.    public void keyPressed(int keyCode) {
  609.       switch (((Canvas)this).getGameAction(keyCode)) {
  610.          case 1:
  611.             this.Rotate();
  612.             break;
  613.          case 2:
  614.             this.CalculateLeft();
  615.          case 3:
  616.          case 4:
  617.          default:
  618.             break;
  619.          case 5:
  620.             this.CalculateRight();
  621.             break;
  622.          case 6:
  623.             this.iCurrentSpeed = this.iDropSpeed;
  624.             if (this.iTimerSem == 0 && this.iEndGame == 0) {
  625.                this.tetTimer.cancel();
  626.                this.dropTimer = new Timer();
  627.                this.dropTimer.schedule(new TetrisTask(this), 0L, (long)this.iDropSpeed);
  628.                ++this.iTimerSem;
  629.             }
  630.       }
  631.  
  632.    }
  633. }
  634.